package com.joven.shelltest.controller;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sun.font.FontDesignMetrics;
import sun.misc.BASE64Encoder;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 获取系统字体
 * @Author wzj
 * @Date 2023-09-06 11:23
 **/
@RestController
@RequestMapping("/fonts")
public class FontController {

    @GetMapping(value = "/getFont")
    public ResponseEntity<Object> getFont() {
        //系统所有字体系列名称的数组
        GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
        String[] fontNames = e.getAvailableFontFamilyNames();
        java.util.List<String> result = new ArrayList<>();
        for (String fontName : fontNames) {
            System.out.println(fontName);
            result.add(fontName);
        }

        java.util.List<String> result1 = new ArrayList<>();
        //获取所有字体样式数组
        GraphicsEnvironment e1 = GraphicsEnvironment.getLocalGraphicsEnvironment();
        Font[] fonts = e1.getAllFonts();
        for (Font font : fonts) {
            String fontName = font.getFontName();
            result1.add(fontName);
        }
        Map<String, List<String>> map = new HashMap<>(16);
        map.put("fontNames", result);
        map.put("fontName", result1);
        return new ResponseEntity<>(map, HttpStatus.OK);
    }


    @GetMapping(value = "/getFontTest")
    public ResponseEntity<Object> getFontTest() throws IOException {
        int fontSize = 20;
        // 获取当前系统所有字体
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        String[] fontNames = ge.getAvailableFontFamilyNames();
        String str = "东临碣石，以观沧海。水何澹澹，山岛竦峙。";
        int height = 0;
        int width = 0;
        // 根据字体获取需要生成的图片的宽和高
        for (String fontName : fontNames) {
            Font font = new Font(fontName, Font.PLAIN, fontSize);
            FontDesignMetrics metrics = FontDesignMetrics.getMetrics(font);
            height += metrics.getHeight();
            int tmpWidth = metrics.stringWidth(fontName + " : " + str);
            if (tmpWidth > width) {
                width = tmpWidth;
            }
        }
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);

        Graphics2D g2d = null;
        try {
            //创建画笔
            g2d = image.createGraphics();
            //设置背景颜色为白色
            g2d.setColor(Color.WHITE);
            g2d.fillRect(0, 0, image.getWidth(), image.getHeight());
            //设置画笔颜色为黑色
            g2d.setColor(Color.BLACK);
            RenderingHints hints = new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
            // 开启文字抗锯齿
            hints.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
            g2d.setRenderingHints(hints);
            int startY = 0;
            for (String fontName : fontNames) {
                Font font = new Font(fontName, Font.PLAIN, fontSize);
                System.out.println(fontName);
                g2d.setFont(font);
                g2d.drawString(fontName + " : " + str, 0, startY);
                // 下一行文字的左上角纵坐标
                FontDesignMetrics metrics = FontDesignMetrics.getMetrics(font);
                startY += metrics.getHeight();
            }

            ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
            ImageIO.write(image, "jpg", baosOut);

            //转换成字节
            byte[] bytes = baosOut.toByteArray();
            BASE64Encoder encoder = new BASE64Encoder();
            //转换成base64串
            String pngBase64 = encoder.encodeBuffer(bytes).trim();
            //删除 \r\n
            pngBase64 = pngBase64.replaceAll("\n", "").replaceAll("\r", "");

            return new ResponseEntity<>(pngBase64, HttpStatus.OK);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (g2d != null) {
                g2d.dispose();
            }
        }
        return null;
    }

}
